const Koa = require("koa")
const app = new Koa()
const views = require("koa-views")
const json = require("koa-json")
const onerror = require("koa-onerror")
const bodyparser = require("koa-bodyparser")
const logger = require("koa-logger")
const session = require("koa-generic-session")
const cors = require("koa2-cors")

const index = require("./routes/index")
const users = require("./routes/users")
const addressRouter = require("./routes/address")
const shopRouter = require("./routes/shop")
const orderRouter = require("./routes/order")

// error handler
onerror(app)

// cors配置
app.use(
  cors({
    origin: "http://localhost:8080",
    // origin: "http://192.168.1.5:8080"
    credentials: true //允许跨域带着 cookie
  })
)

// middlewares
app.use(
  bodyparser({
    enableTypes: ["json", "form", "text"]
  })
)
app.use(json())
app.use(logger())
app.use(require("koa-static")(__dirname + "/public")) //public静态文件夹

app.use(
  views(__dirname + "/views", {
    extension: "pug"
  })
)
// session配置
app.keys = ["wertwe^&&*UUI123123"] // 秘钥，用于加密
// 自定配置了 cookie 和 session
app.use(
  session({
    // 配置 cookie
    cookie: {
      path: "/", // cookie 在根目录下有效
      httpOnly: true, // cookie 只允许服务端来操作
      maxAge: 24 * 60 * 60 * 1000 // cookie 的过期时间
    }
  })
)

// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())
app.use(addressRouter.routes(), addressRouter.allowedMethods())
app.use(shopRouter.routes(), shopRouter.allowedMethods())
app.use(orderRouter.routes(), orderRouter.allowedMethods())

// error-handling
app.on("error", (err, ctx) => {
  console.error("server error", err, ctx)
})

module.exports = app
